package com.ruoyi.system.utils;

import com.ruoyi.system.domain.ShareK;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.math.BigDecimal;

public class AllTickUtils {

    public static final String TOKEN = "06cad05e9e79f6903dbe380ef2ee686b-c-app";

    /**
     * 获取股票最新数据
     * @param code 股票代码
     * @return 响应字符串，如果发生异常则返回 null
     */
    public static ShareK getAllTickData(String code,String shareName) {
        String url = "https://quote.tradeswitcher.com/quote-stock-b-api/trade-tick?token="+TOKEN+"&query=";
        String jsonData = "{\"trace\": \"edd5df80-df7f-4acf-8f67-68fd2f096426\",\"data\": {\"symbol_list\": [{\"code\": \""+code+"\"}]}}";
        String allTickData = allTickHttpPost(url, jsonData);
        //解析数据
        JsonObject jsonObject = new JsonObject(allTickData);
        System.out.println(jsonObject);
        JsonObject dataObject = jsonObject.getJsonObject("data");
        JsonArray tickListArray = dataObject.getJsonArray("tick_list");

        JsonObject jsonObject1 = tickListArray.getJsonObject(0);
        String codeValue = jsonObject1.getString("code");
        String tick_time = jsonObject1.getString("tick_time");
        String price = jsonObject1.getString("price");
        String volume = jsonObject1.getString("volume");

        ShareK shareK = new ShareK();
        shareK.setCode(codeValue);
        shareK.setTickTime(tick_time);
        shareK.setPrice(new BigDecimal(price));
        shareK.setVolume(volume);
        shareK.setStockName(shareName);
        return shareK;
    }

    /**
     * AllTickK线数据获取
     * @param code 股票代码
     * @return
     */
    public static String getAllTickKData(String code){
        String url = "https://quote.tradeswitcher.com/quote-stock-b-api/kline?token="+TOKEN+"&query=";
        String jsonData = "{\n" +
                "  \"trace\": \"3baaa938-f92c-4a74-a228-fd49d5e2f8bc-1678419657806\",\n" +
                "  \"data\": {\n" +
                "    \"code\": \""+code+"\",\n" +
                "    \"kline_type\": 1,\n" +
                "    \"kline_timestamp_end\": 0,\n" +
                "    \"query_kline_num\": 1,\n" +
                "    \"adjust_type\": 0\n" +
                "  }\n" +
                "}";
        return  allTickHttpPost(url, jsonData);

    }

    /**
     * http请求公共方法
     * @param url
     * @param jsonData
     * @return
     */
    public static String allTickHttpPost(String url, String jsonData) {
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpPost httpPost = new HttpPost(url + java.net.URLEncoder.encode(jsonData, "UTF-8"));

            try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
                HttpEntity entity = response.getEntity();
                String responseString = EntityUtils.toString(entity);
                return responseString;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}